/*
 * Copyright (c) Jiandong.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */

import hilog from '@ohos.hilog';
import Want from '@ohos.application.Want';
import Window from '@ohos.window'

import { ReactInstanceManager } from "./ReactInstanceManager"
import { ReactNativeHost } from "./ReactNativeHost"

export class ReactDelegate {

    private static readonly defaultHarmonyPageName: string = "pages/index"
    private static readonly TAG: string = "ReactDelegate"
    private mWant: Want
    private mLaunchParam: any

    // TODO , just process the case: there is just one single ability per a harmony application
    public constructor(private mMainComponentName?: string, private mHarmonyPageName?: string) { }

    private getReactInstanceManager(): ReactInstanceManager {
        return ReactNativeHost.getInstance().getReactInstanceManager();
    }

    public onHostCreate(want: Want, launchParam: any) {
       this.mWant = want
       this.mLaunchParam = launchParam
       this.getReactInstanceManager()?.onHostCreate(want, launchParam)
    }

    public onHostDestroy() {
        this.getReactInstanceManager()?.onHostDestroy();
    }

    public onHostWindowStageCreate(windowStage: Window.WindowStage) {
        this.getReactInstanceManager()?.createHarmonyRootComponent(windowStage, this.mHarmonyPageName)
        this.getReactInstanceManager()?.startReactFramework(this.mMainComponentName, this.mWant, this.mLaunchParam);
    }

    public onHostWindowStageDestroy() {
        this.getReactInstanceManager()?.onHostWindowStageDestroy();
    }

    public onHostForeground() {
        this.getReactInstanceManager()?.onHostForeground();
    }

    public onHostBackground() {
        this.getReactInstanceManager()?.onHostBackground();
    }
}
